package com.cxb.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.cxb.manager.PhoneManager;
import com.cxb.utils.Constants;

public class PhoneStateReceiver extends BroadcastReceiver {
	Context context = null;
	String phoneNumber = null;

	@Override
	public void onReceive(Context mContext, Intent intent) {

		context = mContext;
		PhoneManager phoneManager = new PhoneManager(mContext);
		PhoneManager.PhoneState currentPhoneState = PhoneManager.getCallState();

		if (intent.getAction().equals(
				Constants.IntentData.ACTION_PHONE_STATE_INTENT)) {// 自定义电话状态广�?
			String phoneState = intent.getStringExtra("state");

			Log.i("test", "phone state change phoneState="+phoneState );
			if (PhoneManager.getCallState() == PhoneManager.PhoneState.RING
					&& !"ACTIVE".equals(phoneState)) {// 来电不在这里处理
				return;
			}

			if ("DIALING".equals(phoneState)) {// 拨出
//				currentPhoneState = PhoneManager.PhoneState.DIALING;
			} else if ("ACTIVE".equals(phoneState)) {
				if (currentPhoneState == PhoneManager.PhoneState.DIALING) {
					// 拨出接�?
				} else {
					// 来电接�?
				}
				currentPhoneState = PhoneManager.PhoneState.ACTIVE;

			} else if ("DISCONNECTED".equals(phoneState)) {// 挂断
				currentPhoneState = PhoneManager.PhoneState.IDLE;
				PhoneManager.setConectingPhoneNumber(null);
				if (PhoneManager.gpsDestination != null) {
					String intInfo = "nav:"
							+ PhoneManager.gpsDestination.destName + ","
							+ PhoneManager.gpsDestination.longitude + ","
							+ PhoneManager.gpsDestination.latitude;
					Intent i = new Intent(Intent.ACTION_VIEW,
							Uri.parse(intInfo));
					i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(i);
					PhoneManager.gpsDestination = null;
					Log.e("ares", intInfo);
				}
				Log.e("ares", "no gpsdestination");
			}
		} else if (intent.getAction().equals(
				TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
			// 如果是来电
			TelephonyManager tm = (TelephonyManager) mContext
					.getSystemService(Context.TELEPHONY_SERVICE);
			Log.i("test", "phone State="+tm.getCallState());

			if (tm.getCallState() == TelephonyManager.CALL_STATE_RINGING) {
//				if (PhoneManager.getCallState() == PhoneManager.PhoneState.IDLE) {
					phoneNumber = intent
							.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
					if (phoneNumber == null)
						phoneNumber = "未知号码";// 有时来电号码为空
					PhoneManager.setConectingPhoneNumber(phoneNumber);
					currentPhoneState = PhoneManager.PhoneState.RING;
//				} else {// 挂断第三方通话
//					Class<TelephonyManager> c = TelephonyManager.class;
//					Method getITelephonyMethod = null;
//					try {
//						getITelephonyMethod = c.getDeclaredMethod(
//								"getITelephony", (Class[]) null);
//						getITelephonyMethod.setAccessible(true);
//
//						TelephonyManager tManager = (TelephonyManager) context
//								.getSystemService(Context.TELEPHONY_SERVICE);
//						Object iTelephony = getITelephonyMethod.invoke(
//								tManager, (Object[]) null);
//						Method endCall = iTelephony.getClass()
//								.getDeclaredMethod("endCall");
//						endCall.invoke(iTelephony);
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
			} else if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
				currentPhoneState = PhoneManager.PhoneState.IDLE;
			} 
		}else if(intent.getAction().equals(
				Intent.ACTION_NEW_OUTGOING_CALL)){
			 String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
			 PhoneManager.setConectingPhoneNumber(phoneNumber);
			 currentPhoneState = PhoneManager.PhoneState.DIALING;
		}

//		phoneManager.setCallState(currentPhoneState);
//		CxbEventBus.getCxbBus(CxbEventBus.EVENT_PHONE_STATE_CHANGE).post(
//				currentPhoneState);
//		if (currentPhoneState != PhoneState.IDLE)
//			PhoneDialogView.getInstance(mContext).show();
	}
}
